function flattenVNodes (children) {
  const result = []
  const traverse = (vnodes) => {
    vnodes.forEach((vnode) => {
      result.push(vnode)
      if (vnode.componentInstance) { traverse(vnode.componentInstance.$children.map(item => item.$vnode)) }

      if (vnode.$children) { traverse(vnode.$children) }
    })
  }
  traverse(children)
  return result
}

export function flatten (instance) {
  if (!instance.$vnode) { return [] }
  const { componentInstance } = instance.$vnode
  if (!componentInstance || !componentInstance.$children) { return [] }

  const VNodes = flattenVNodes(componentInstance.$children)
  return VNodes
}
